package com.css.fxfzfxqh.modules.lifelineEngineering.repository.impl;

import com.css.fxfzfxqh.base.dict.repository.entity.DictEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.EvaluationResults;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.EvaluationResultsEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineEstimateEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.SmxVectorEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.repository.EstimateRepository;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class EstimateRepositoryImpl implements EstimateRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<String> getCenterPoint(String geom) {
        List<String> centerLonLat = new ArrayList<>();
        if (PlatformObjectUtils.isNotEmpty(geom)) {
            String sql = "SELECT st_astext(st_centroid('" + geom + "')) as geom";
            String centerPoint = jdbcTemplate.queryForObject(sql, String.class);
            if (PlatformObjectUtils.isNotEmpty(centerPoint) && centerPoint.indexOf("POINT(") != -1) {
                String lonlatStr = centerPoint.replace("POINT(", "").replace(")", "");
                String[] lonlat = lonlatStr.split(" ");
                if (lonlat.length == 2) {
                    centerLonLat.add(lonlat[0]);//经度
                    centerLonLat.add(lonlat[1]);//纬度
                }
            }
        }
        return centerLonLat;
    }

    @Override
    public void save(EvaluationResults entity) {
        String sql = "insert into fxqh_smx_evaluation_results values (:id,:taskId,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10,:extends11,:type,:disasterRate6State,:disasterRate6StateCorrection,:disasterRate7State,:disasterRate7StateCorrection,:disasterRate8State,:disasterRate8StateCorrection,:disasterRate9State,:disasterRate9StateCorrection,:disasterRate10State,:disasterRate10StateCorrection,:delFlag,:dataSourceId,:disasterRateState,:disasterRateStateCorrection,ST_GeometryFromText(:geom,4490),:rang,:functionalFailure,:functionalFailureCorrect,:functionalFailure6,:functionalFailure6Correct,:functionalFailure7,:functionalFailure7Correct,:functionalFailure8,:functionalFailure8Correct,:functionalFailure9,:functionalFailure9Correct,:functionalFailure10,:functionalFailure10Correct,:longitude,:latitude,:versionCode)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public int getByTaskIdAndType(String id,Integer type) {
        String sql = "select count(1) from fxqh_smx_evaluation_results where task_id = '"+id+"' and del_flag = '0' and type = "+type;
        return jdbcTemplate.queryForObject(sql,Integer.class);
    }

    @Override
    public void deleteByTaskIdAndType(String id,Integer type) {
        String sql = "delete from fxqh_smx_evaluation_results where task_id = '"+id+"' and type = "+type;
        jdbcTemplate.update(sql);
    }

    @Override
    public List<EvaluationResults> getListByTaskId(String id) {
        String sql = "select * from fxqh_smx_evaluation_results where task_id = '"+id+"' and del_flag = '0'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(EvaluationResults.class));
    }

    @Override
    public void deleteByTaskId(String id) {
        String sql = "update fxqh_smx_evaluation_results set del_flag = '0' where task_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<EvaluationResults> getListByTaskIdAndType(String taskId,Integer type) {
        String sql = "select * from fxqh_smx_evaluation_results where task_id = '"+taskId+"' and type ="+type;
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(EvaluationResults.class));
    }

    @Override
    public SmxVectorEntity getVectorByVersionId(String versionId,Integer type) {
        try {
            String table = "fxqh_smx_vector_base_station";
            if(type == 6){
                table = "fxqh_smx_vector_ts";
            }
            String sql = "select * from "+table+" where version_id = '"+versionId+"'";
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SmxVectorEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public String getCenterGeomByTaskIdAndType(String taskId, List<Integer> catalogNameList,String tableName) {
//        String sql = " select ST_AsText(ST_Centroid(ST_extent(geom)))  FROM fxqh_smx_evaluation_results where type ='"+type+"' and task_id='"+taskId+"'";
//        List<String> strings = jdbcTemplate.queryForList(sql, String.class);
//        return PlatformObjectUtils.isNotEmpty(strings)?strings.get(0):"";
        StringBuilder sql = new StringBuilder("select ST_AsText(ST_Centroid(ST_extent(geom)))  FROM "+tableName+" where type in (:ids) and task_id='"+taskId+"'");
        Map idMap = new HashMap();
        idMap.put("ids", catalogNameList);
        return namedParameterJdbcTemplate.queryForObject(sql.toString(), idMap, String.class);
    }

    @Override
    public String getExtentGeomByTaskIdAndType(String taskId, List<Integer> catalogNameList,String tableName) {
//        String sql = " select ST_extent(geom)  FROM fxqh_smx_evaluation_results where type ='"+type+"' and task_id='"+taskId+"'";
//        List<String> strings = jdbcTemplate.queryForList(sql, String.class);
//        return PlatformObjectUtils.isNotEmpty(strings)?strings.get(0):"";

        StringBuilder sql = new StringBuilder("select ST_extent(geom)  FROM "+tableName+" where type in (:ids) and task_id='"+taskId+"'");
        Map idMap = new HashMap();
        idMap.put("ids", catalogNameList);
        return namedParameterJdbcTemplate.queryForObject(sql.toString(), idMap, String.class);
    }

    @Override
    public void saveLine(EvaluationResults entity) {
        String sql = "insert into fxqh_smx_evaluation_results_line values (:id,:taskId,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10,:extends11,:type,:disasterRate6State,:disasterRate6StateCorrection,:disasterRate7State,:disasterRate7StateCorrection,:disasterRate8State,:disasterRate8StateCorrection,:disasterRate9State,:disasterRate9StateCorrection,:disasterRate10State,:disasterRate10StateCorrection,:delFlag,:dataSourceId,:disasterRateState,:disasterRateStateCorrection,ST_GeometryFromText(:geom,4490),:rang,:functionalFailure,:functionalFailureCorrect,:functionalFailure6,:functionalFailure6Correct,:functionalFailure7,:functionalFailure7Correct,:functionalFailure8,:functionalFailure8Correct,:functionalFailure9,:functionalFailure9Correct,:functionalFailure10,:functionalFailure10Correct,:longitude,:latitude,:versionCode)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<EvaluationResults> getLineListByTaskIdAndType(String taskId, Integer type) {
        String sql = "select * from fxqh_smx_evaluation_results_line where task_id = '"+taskId+"' and type ="+type;
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(EvaluationResults.class));
    }

    @Override
    public List<EvaluationResults> getLineListByTaskId(String id) {
        String sql = "select * from fxqh_smx_evaluation_results_line where task_id = '"+id+"' and del_flag = '0'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(EvaluationResults.class));
    }

    @Override
    public void deleteLineByTaskIdAndType(String id, Integer type) {
        String sql = "delete from fxqh_smx_evaluation_results_line where task_id = '"+id+"' and type = "+type;
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteLineByTaskId(String id) {
        String sql = "update fxqh_smx_evaluation_results_line set del_flag = '0' where task_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<String> getTypeListByTaskId(String taskId) {
        String sql = "SELECT distinct(type) FROM fxqh_smx_evaluation_results where  task_id='"+taskId+"'" +
                "union " +
                "SELECT distinct(type) FROM fxqh_smx_evaluation_results_line where  task_id='"+taskId+"'";
        return jdbcTemplate.queryForList(sql,String.class);
    }

    @Override
    public int getLineByTaskIdAndType(String id, Integer type) {
        String sql = "select count(1) from fxqh_smx_evaluation_results_line where task_id = '"+id+"' and del_flag = '0' and type = "+type;
        return jdbcTemplate.queryForObject(sql,Integer.class);
    }
}
